﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BE;
using BE.SearchCriteria;

namespace DAL
{
    public class DivisionDAL : BaseDAL
    {
        public void Insertar(DivisionBE pElemBE)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Divisiones_i";                    
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pElemBE.Descripcion));

                    SqlParameter p = new SqlParameter();
                    p.ParameterName = "@IdDivision";
                    p.Direction = ParameterDirection.Output;
                    p.SqlDbType = SqlDbType.Int;
                    cmd.Parameters.Add(p);

                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    pElemBE.IdDivision = (Int32)cmd.Parameters["@IdDivision"].Value;
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Modificar(DivisionBE pElemBE)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Divisiones_u";
                    cmd.Parameters.Add(new SqlParameter("@IdDivision", pElemBE.IdDivision));
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pElemBE.Descripcion));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Delete(int pIdDivision)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Divisiones_d";
                    cmd.Parameters.Add(new SqlParameter("@IdDivision", pIdDivision));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }

        }

        public List<DivisionBE> SeleccionarTodos()
        {
            List<DivisionBE> listaDeDivisiones = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "Divisiones_GetAll";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    listaDeDivisiones = new List<DivisionBE>();

                    while (reader.Read())
                    {
                        listaDeDivisiones.Add(Construir(reader));
                    }
                }
                reader.Close();                
            }
            return listaDeDivisiones;
        }

        private DivisionBE Construir(SqlDataReader pReader)
        {
            DivisionBE oDivision = new DivisionBE();

            oDivision.IdDivision = Convert.ToInt32(pReader["IdDivision"]);
            oDivision.Descripcion = Convert.ToString(pReader["Descripcion"]);            

            return oDivision;
        }

    }
}
